home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / dm3_src.zip / DMDOS.C < prev    next >
Text File  |  1990-05-18  |  15KB  |  501 lines

  1. /* ************************************************************************* */
  2. /*                                                                           */
  3. /*       D O O R W A R E   D O S   I N T E R F A C E   L I B R A R Y         */
  4. /*                                                                           */
  5. /*                         For Mycrosoft & Turbo C                           */
  6. /*                                                                           */
  7. /* ************************************************************************* */
  8. /*                                                                           */
  9. /*      This module contains functions used to input and output info         */
  10. /*      with the user and  remote_users console.  Additionally, it           */
  11. /*      contains functions that interface with DOS.                          */
  12. /*                                                                           */
  13. /*      The following funtions are contained in this file:                   */
  14. /*                                                                           */
  15. /*              get_dta         - Get Disk Transfer Address                  */
  16. /*              fsearch         - Search for files                           */
  17. /*                                                                           */
  18. /*                                                                           */
  19. /* ************************************************************************* */
  20.  
  21.  
  22.  
  23. /* ************************************************************************* */
  24. /*                            PROGRAM HISTORY                                */
  25. /* ************************************************************************* */
  26. /* 06/20/87     Version 1.00                                                 */
  27. /*                                                                           */
  28. /*                                                                           */
  29. /*                                                                           */
  30. /*                                                                           */
  31. /* ************************************************************************* */
  32.  
  33.  
  34.  
  35. #include "dmcfg.h"                              /* Std defines & includes    */
  36. #include "dmdata.h"                             /* Std DM storage            */
  37.  
  38.  
  39.  
  40. /*
  41.  *
  42.  * Local module storage
  43.  *
  44.  */
  45.  
  46.  
  47. /*
  48.  *
  49.  * Open a file
  50.  *
  51.  */
  52.  
  53. file_open(file, rw, mode, cflag)                /* Open a file               */
  54.  
  55. FS      *file;                                  /* File access structure     */
  56. int     rw;                                     /* 0 = read / 1 = modify     */
  57.                                                 /* 2 = append / 3 = write    */
  58. int     mode;                                   /* 0 = text / 1 = binary     */
  59. int     cflag;                                  /* Create if not exist       */
  60. {
  61. int     type;
  62. int     c1flag;
  63. int     share;
  64. int     share_open;
  65. char    stype[10];
  66.  
  67.  
  68.    type = 0;
  69.    c1flag = 1;
  70.    strcpy(stype, "\0");
  71.  
  72.    switch(rw)
  73.    {
  74.      case 0:    /* Read */
  75.                 type  = type | O_RDONLY;
  76. #if COMPILER == MSC
  77.                 share = SH_DENYNO;
  78. #endif
  79. #if COMPILER == BTC
  80.                 share = O_DENYNONE;
  81. #endif
  82.                 strcat(stype, "r");
  83.                 break;
  84.  
  85.      case 1:    /* Modify */
  86.                 type  = type | O_RDWR;
  87. #if COMPILER == MSC
  88.                 share = SH_DENYWR;
  89. #endif
  90. #if COMPILER == BTC
  91.                 share = O_DENYWRITE;
  92. #endif
  93.                 strcat(stype, "r+");
  94.                 break;
  95.  
  96.      case 2:    /* Append */
  97.                 type  = type | O_APPEND;
  98. #if COMPILER == MSC
  99.                 share = SH_DENYWR;
  100. #endif
  101. #if COMPILER == BTC
  102.                 share = O_DENYWRITE;
  103. #endif
  104.                 strcat(stype, "a+");
  105.                 break;
  106.  
  107.      case 3:    /* Write */
  108.                 type  = type | O_RDWR | O_TRUNC;
  109. #if COMPILER == MSC
  110.                 share = SH_DENYWR;
  111. #endif
  112. #if COMPILER == BTC
  113.                 share = O_DENYWRITE;
  114. #endif
  115.                 strcat(stype, "w+");
  116.                 break;
  117.    }
  118.  
  119.    if(mode)
  120.    {
  121.      type = type | O_BINARY;
  122.      strcat(stype, "b");
  123.    }
  124.  
  125.  
  126.    if(_osmajor >= 3)
  127.    {
  128.      share_open = 1;
  129.      file->open_flag = -3;
  130.    }
  131.    else
  132.    {
  133.      share_open = 0;
  134.      file->open_flag = -1;
  135.    }
  136.  
  137.  
  138.    while(file->open_flag < 0)
  139.    {
  140.      if((_osmajor >= 3) && (share_open))
  141.      {
  142. #if COMPILER == MSC
  143.        file->fh = sopen(file->name, type, share, S_IREAD | S_IWRITE);
  144. #endif
  145. #if COMPILER == BTC
  146.        file->fh = open(file->name, type | share, S_IREAD | S_IWRITE);
  147. #endif
  148.      }
  149.      else
  150.        file->fh = open(file->name, type, S_IREAD | S_IWRITE);
  151.  
  152.      if(file->fh == -1)
  153.      {
  154.        switch(errno)
  155.        {
  156.          case EACCES:   /* Access violation */
  157.                         file->open_flag++;
  158.                         break;
  159.  
  160.          case EEXIST:   /* File already exists */
  161.                         file->open_flag = 0;
  162.                         return(-1);
  163.                         break;
  164.  
  165.          case EINVAL:   /* SHARE.COM not installed */
  166.                         share_open = 0;
  167.                         break;
  168.  
  169.          case EMFILE:   /* No handles available */
  170.                         file->open_flag = 0;
  171.                         return(-2);
  172.                         break;
  173.  
  174.          case ENOENT:   /* File not found */
  175.                         if(cflag && c1flag)
  176.                         {
  177.                           type = type | O_CREAT;
  178.                           stype[0] = 'w';
  179.                           c1flag = 0;
  180.                           file->open_flag--;
  181.                         }
  182.                         else
  183.                         {
  184.                           file->open_flag = 0;
  185.                           return(-3);
  186.                         }
  187.                         break;
  188.        }
  189.      }
  190.      else
  191.      {
  192.        file->open_flag = rw + 1;
  193.        file->binary    = mode;
  194.  
  195.        file->fd = fdopen(file->fh, stype);
  196.        if(file->fd == 0L)
  197.        {
  198.          close(file->fh);
  199.          file->open_flag = 0;
  200.          return(-4);
  201.        }
  202.        return(0);
  203.      }
  204.    }
  205.    file->open_flag = 0;
  206.    return(-5);
  207. }
  208.  
  209.  
  210. /*
  211.  *
  212.  * Close a file
  213.  *
  214.  */
  215.  
  216. file_close(file)                                /* Open a file               */
  217.  
  218. FS      *file;
  219. {
  220.    fclose(file->fd);
  221.    close(file->fh);
  222.    file->open_flag = 0;
  223.    return(0);
  224. }
  225.  
  226.  
  227. /*
  228.  *
  229.  * Change current file access
  230.  *
  231.  */
  232.  
  233. file_change(file, rw, mode, cflag)              /* Open a file               */
  234.  
  235. FS      *file;
  236. int     mode;                                   /* 0 = text, 1 = binary      */
  237. int     cflag;                                  /* 1 = create if not exist   */
  238. {
  239. long    ftell();
  240.  
  241.    file->hold_flag = file->open_flag;           /* Save previous state       */
  242.    file->hold_mode = file->binary;
  243.  
  244.    if(file->open_flag)                          /* if file was open          */
  245.    {
  246.      file->hold_pos = ftell(file->fd);          /* save previous position    */
  247.      file_close(file);
  248.      return(file_open(file, rw, mode, FNOCREATE)); /* open it up             */
  249.    }
  250.    return(file_open(file, rw, mode, cflag));    /* open it up                */
  251. }
  252.  
  253.  
  254. /*
  255.  *
  256.  * Reset to previous access
  257.  *
  258.  */
  259.  
  260. file_reset(file)                                /* Open a file               */
  261.  
  262. FS      *file;
  263. {
  264. int     stat;
  265.  
  266.    file_close(file);                            /* close it up               */
  267.    stat = file_open(file, file->hold_flag - 1,
  268.           file->hold_mode, FNOCREATE);          /* open it up                */
  269.  
  270.    if(stat)                                     /* exit if error             */
  271.      return(stat);
  272.  
  273.    fseek(file->fd, file->hold_pos, 0);          /* seek back to where we were */
  274.    return(0);
  275. }
  276.  
  277.  
  278. /*
  279.  *
  280.  * Get Disk Transfer Address
  281.  *
  282.  */
  283.  
  284. char    *get_dta()                              /* Pointer to return value   */
  285. {
  286. union   REGS    inregs, outregs;                /* DOS access structure      */
  287. struct  SREGS   segregs;
  288.  
  289. unsigned long   temp;                           /* Work variable             */
  290.  
  291.    inregs.x.ax = 0x2f00;                        /* Get the address           */
  292.    intdosx(&inregs, &outregs, &segregs);
  293.    temp = segregs.es;                           /* Convert to a pointer      */
  294.    temp = temp << 16;
  295.    temp += outregs.x.bx;
  296.    return((char *)temp);                        /* return it                 */
  297. }
  298.  
  299.  
  300. /*
  301.  *
  302.  * Get Files
  303.  *
  304.  */
  305.  
  306. fsearch(name, buffer)
  307.  
  308. char    *name;                                  /* Name to match             */
  309. char    *buffer;                                /* Place to store matches    */
  310. {
  311. char    *get_dta();
  312.  
  313. union   REGS    inregs, outregs;                /* DOS access structure      */
  314. struct  SREGS   segregs;
  315.  
  316. int     count = 0;                              /* Match counter             */
  317. char    *data_ptr;                              /* Search buffer             */
  318. char    *fil_name;                              /* Filename pointer          */
  319.  
  320.    data_ptr = get_dta();                        /* Get DTA address           */
  321.  
  322.    inregs.x.ax = 0x4e00;                        /* Search for 1st match      */
  323.    inregs.x.cx = 0x0000;
  324.    segregs.ds = (unsigned long)name >> 16;
  325.    inregs.x.dx = (unsigned long)name & (unsigned long)0x0000ffff;
  326.  
  327.    intdosx(&inregs, &outregs, &segregs);
  328.    if(outregs.x.ax != 0)                        /* If non found...           */
  329.      return(0);                                 /* ...then exit, 0 matches   */
  330.    else
  331.    {
  332.      fil_name = data_ptr + 30;                  /* Point to filename         */
  333.      while(*fil_name != '\0')                   /* Until we reach the end... */
  334.      {
  335.        *buffer = *fil_name;                     /* ...copy each char         */
  336.        buffer++;                                /* ...bump the pointers      */
  337.        fil_name++;
  338.      }
  339.  
  340.      *buffer = 0;                               /* Terminate this string     */
  341.      buffer++;                                  /* Set for next filename     */
  342.      count++;                                   /* 1 more file matched       */
  343.    }
  344.  
  345.    while(1)                                     /* Until we are done...      */
  346.    {
  347.      inregs.h.ah = 0x4f;                        /* Search for next match     */
  348.      inregs.x.cx = 0x0000;
  349.      segregs.ds = (unsigned long)name >> 16;
  350.      inregs.x.dx = (unsigned long)name & (unsigned long)0x0000ffff;
  351.  
  352.      intdosx(&inregs, &outregs, &segregs);
  353.      if(outregs.x.ax)                           /* If no match...            */
  354.        return(count);                           /* ...exit, with total       */
  355.      else
  356.      {
  357.        fil_name = data_ptr + 30;                /* Point to filename         */
  358.        while(*fil_name != '\0')                 /* Until end of name...      */
  359.        {
  360.          *buffer = *fil_name;                   /* ...copy each char         */
  361.          buffer++;                              /* ...bump the pointers      */
  362.          fil_name++;
  363.        }
  364.        *buffer = 0;                             /* Terminate the filename    */
  365.        buffer++;                                /* Ready for next filename   */
  366.        count++;                                 /* 1 more match              */
  367.      }
  368.    }
  369. }
  370.  
  371.  
  372. /*
  373.  *
  374.  * Get File Date
  375.  *
  376.  */
  377.  
  378. fdate(name, buffer)
  379.  
  380. char    *name;                                  /* Name to match             */
  381. char    *buffer;                                /* Place to store date       */
  382. {
  383. char    *get_dta();
  384.  
  385. union   REGS    inregs, outregs;                /* DOS access structure      */
  386. struct  SREGS   segregs;
  387.  
  388. char    string[10];
  389. unsigned char   *data_ptr;                      /* Search buffer             */
  390. unsigned int    date;                           /* Date value                */
  391. int     year, month, day;
  392.  
  393.  
  394.    data_ptr = (unsigned char *)get_dta();       /* Get DTA address           */
  395.  
  396.    inregs.x.ax = 0x4e00;                        /* Search for 1st match      */
  397.    inregs.x.cx = 0x0000;
  398.    segregs.ds = (unsigned long)name >> 16;
  399.    inregs.x.dx = (unsigned long)name & (unsigned long)0x0000ffff;
  400.  
  401.    intdosx(&inregs, &outregs, &segregs);
  402.    if(outregs.x.ax != 0)                        /* If non found...           */
  403.      return(-1);                                /* ...then exit with error   */
  404.    else
  405.    {
  406.      date  = data_ptr[24];                      /* Get the date value        */
  407.      date += ((unsigned int)(data_ptr[25])) << 8;
  408.  
  409.      year  = (date & 0xfe00) >> 9;              /* seperate the parts        */
  410.      month = (date & 0x01e0) >> 5;
  411.      day   = (date & 0x001f);
  412.  
  413.      switch(month)
  414.      {
  415.        case 1:  strcpy(buffer, "Jan");
  416.                 break;
  417.        case 2:  strcpy(buffer, "Feb");
  418.                 break;
  419.        case 3:  strcpy(buffer, "Mar");
  420.                 break;
  421.        case 4:  strcpy(buffer, "Apr");
  422.                 break;
  423.        case 5:  strcpy(buffer, "May");
  424.                 break;
  425.        case 6:  strcpy(buffer, "Jun");
  426.                 break;
  427.        case 7:  strcpy(buffer, "Jul");
  428.                 break;
  429.        case 8:  strcpy(buffer, "Aug");
  430.                 break;
  431.        case 9:  strcpy(buffer, "Sep");
  432.                 break;
  433.        case 10: strcpy(buffer, "Oct");
  434.                 break;
  435.        case 11: strcpy(buffer, "Nov");
  436.                 break;
  437.        case 12: strcpy(buffer, "Dec");
  438.                 break;
  439.      }
  440.      sprintf(string, " %2d", day);
  441.      strcat(buffer, string);
  442.      sprintf(string, " %4d\n", year + 1980);
  443.      strcat(buffer, string);
  444.    }
  445.    return(0);
  446. }
  447.  
  448.  
  449. /*
  450.  *
  451.  * Hide File
  452.  *
  453.  */
  454.  
  455. fhide(filename)
  456.  
  457. char    *filename;                              /* Name of file              */
  458. {
  459. union   REGS    inregs, outregs;                /* DOS access structure      */
  460. struct  SREGS   segregs;
  461.  
  462.    inregs.x.ax = 0x4301;                        /* Hide the file             */
  463.    inregs.x.cx = 0x0002;
  464.    segregs.ds = (unsigned long)filename >> 16;
  465.    inregs.x.dx = (unsigned long)filename & (unsigned long)0x0000ffff;
  466.  
  467.    intdosx(&inregs, &outregs, &segregs);
  468.    if(outregs.x.ax != 0)                        /* If not set...             */
  469.      return(-1);                                /* ...then exit with error   */
  470.    else
  471.      return(0);
  472. }
  473.  
  474.  
  475. /*
  476.  *
  477.  * UnHide File
  478.  *
  479.  */
  480.  
  481. funhide(filename)
  482.  
  483. char    *filename;                              /* Name of file              */
  484. {
  485. union   REGS    inregs, outregs;                /* DOS access structure      */
  486. struct  SREGS   segregs;
  487.  
  488.    inregs.x.ax = 0x4301;                        /* UnHide the file           */
  489.    inregs.x.cx = 0x0000;
  490.    segregs.ds = (unsigned long)filename >> 16;
  491.    inregs.x.dx = (unsigned long)filename & (unsigned long)0x0000ffff;
  492.  
  493.    intdosx(&inregs, &outregs, &segregs);
  494.    if(outregs.x.ax != 0)                        /* If not set...             */
  495.      return(-1);                                /* ...then exit with error   */
  496.    else
  497.      return(0);
  498. }
  499.  
  500.  
  501.